import {_decorator, Component, instantiate, Label, Node, Prefab, resources} from 'cc';
import {lobby} from '../../lobby';
import {boot} from "db://assets/boot/boot";

const {ccclass, property} = _decorator;

export function showAlert(title: string, content: string, cb?: () => void, errCb?: () => void) {
    const prefab = resources.get("prefab/alert", Prefab)!
    const alert = instantiate(prefab)
    lobby.instance.alert.addChild(alert);
    alert.getComponent(Alert)!.showAlert(title, content, cb, errCb)
}

@ccclass('alert')
export class Alert extends Component {
    @property(Label)
    title!: Label;

    @property(Label)
    content!: Label;

    @property(Node)
    blackBg!: Node

    @property(Node)
    btnok!: Node;

    @property(Node)
    btncancel!: Node;

    showAlert(title: string, content: string, cb?: () => void, errCb?: () => void) {
        this.title.string = title
        this.content.string = content
        this.btnok.on(Node.EventType.TOUCH_END, () => {
            this.node.destroy()
            cb && cb()
        })
        this.btncancel.on(Node.EventType.TOUCH_END, () => {
            this.node.destroy()
            errCb && errCb()
        })
        this.blackBg.on(Node.EventType.TOUCH_END, () => {
            this.node.destroy()
            errCb && errCb()
        })
    }

    update(deltaTime: number) {

    }
}


